package mac5855.app.controllers;

import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mac5855.app.dao.DAOFactory;
import mac5855.app.dao.PerguntaDAO;
import mac5855.app.dao.RespostaDAO;
import mac5855.app.dao.UsuarioDAO;
import mac5855.app.models.Pergunta;
import mac5855.app.models.Resposta;
import mac5855.app.models.Usuario;

/**
 * Servlet implementation class Pergunta
 */
public class RespostaController implements BusinessLogic {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RespostaController() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub

		String action = request.getParameter("action");
		System.out.println("action:" + action);
		if (action == null) {

			System.out.println("Executando a logica e redirecionando...");
			HttpSession session = ((HttpServletRequest) request).getSession();
			String userEmail = (String) session.getAttribute("User");

			System.out.println("Email user:" + userEmail);

			UsuarioDAO usuarioDAO = DAOFactory.getDAOFactory(1).getUsuarioDAO();
			Usuario usuario = usuarioDAO.findByEmail(userEmail);
			System.out.println(usuario);

			Resposta resposta = new Resposta();
			resposta.setData(new Date());

			PerguntaDAO perguntaDAO = DAOFactory.getDAOFactory(1).getPerguntaDAO();
			Pergunta pergunta = new Pergunta();
			pergunta = perguntaDAO.findById(Integer.parseInt(request.getParameter("pergunta_id")));
			resposta.setPergunta(pergunta);

			resposta.setTitulo(request.getParameter("titulo"));
			resposta.setUsuario(usuario);

			RespostaDAO respostaDAO = DAOFactory.getDAOFactory(1).getRespostaDAO();
			respostaDAO.create(resposta);

			RequestDispatcher rd = request.getRequestDispatcher("/show_pergunta.jsp?id=" + pergunta.getId());
			rd.forward(request, response);
		} else if (action.equals("show")) {
			String id = request.getParameter("id");
			RequestDispatcher rd = request.getRequestDispatcher("/show_pergunta.jsp?id=" + id);
			rd.forward(request, response);
		} else if (action.equals("vote_through")) {
			int id = Integer.parseInt(request.getParameter("id"));
			RespostaDAO respostaDAO = DAOFactory.getDAOFactory(1).getRespostaDAO();
			Pergunta pergunta = respostaDAO.findById(id).getPergunta();
			respostaDAO.vote(id, true);
			RequestDispatcher rd = request.getRequestDispatcher("/show_pergunta.jsp?id=" + pergunta.getId());
			rd.forward(request, response);
		} else if (action.equals("vote_down")) {
			int id = Integer.parseInt(request.getParameter("id"));
			RespostaDAO respostaDAO = DAOFactory.getDAOFactory(1).getRespostaDAO();
			Pergunta pergunta = respostaDAO.findById(id).getPergunta();
			respostaDAO.vote(id, false);
			RequestDispatcher rd = request.getRequestDispatcher("/show_pergunta.jsp?id=" + pergunta.getId());
			rd.forward(request, response);

		} else if (action.equals("del_Resposta")) {
			int id = Integer.parseInt(request.getParameter("id"));
			RespostaDAO respostaDAO = DAOFactory.getDAOFactory(1).getRespostaDAO();
			respostaDAO.delete(id);

			RequestDispatcher rd = request.getRequestDispatcher("/");
			rd.forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

}
